// pages/waitingRentRoom/waitingRentRoom.js
var common = require('../../utils/common.js');
var that = "";


//时间控件
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",//房间id
    data: {},//房间数据
    furniture: [],
    //以下为时间选择变量
    years: years,
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: date.getDate(),
    value: [9998, 0, 0],
    modalHidden: true,//默认隐藏moal
  },

  //时间modal改变监听
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },
  //退租
  surrenderOfTenancy: function () {
    that.setData({
      modalHidden: false,
    })
  },
  //modal确认取消
  //确认
  confirm: function () {
    // this.data.feeList[this.data.WEItemIndex] = this.data.WEItem;
    this.setData({
      // feeList: this.data.feeList,
      modalHidden: true,
    })
    wx.navigateTo({
      url: '../surrenderOfTenancy/surrenderOfTenancy?time=' + that.data.year + '-' + that.data.month + '-' + that.data.day + '&id=' + this.data.id+'&room=room',
    })
  },
  //取消
  cancel: function () {
    this.setData({
      modalHidden: true,
    })
  },

  //修改房间
  modifyRoom: function () {
    wx.navigateTo({
      url: '../modifyRoom/modifyRoom?room_id=' + this.data.id,
    })
  },
  //查看房间账单
  roomsBill: function () {
    wx.navigateTo({
      url: '../roomsBill/roomsBill?room_id=' + this.data.id + '&room_name=' + that.data.data.houseName + '-' + that.data.data.roomcode,
    })
  },
  //添加租户
  adduser: function () {
    if (this.data.data.status == 1) {
      wx.navigateTo({
        url: '../contractDetail/contract?contract_id=' + that.data.data.rentalId,
      })
    } else {
      wx.navigateTo({
        url: '../addTenant/addTenant?room_id=' + that.data.id,
      })
    }

  },
  initData: function () {
    var that = this;
    common._get('/wx/room/getroominfo', { roomId: this.data.id },
      //成功
      function (res) {
        console.log(res.data);
        //
        if (res.data.success) {
          that.setData({
            data: res.data.data,
          })
          //设置配置
          if (res.data.data.furniture){
           that.setData({
             furniture: JSON.parse(res.data.data.furniture),
           })
         }
          //设置标题
          wx.setNavigationBarTitle({
            title: that.data.data.houseName + '-' + that.data.data.roomcode,
          })

        }


      });

  },
  //添加账单
  addzd: function () {
    wx.navigateTo({
      url: '../receiptModify/receiptModify?rooms_id='+this.data.id,
    })
  },
  //删除房间
  deleteRooms: function () {
    var that = this;
    console.log(that.data.id)
    wx.showModal({
      title: '提示',
      content: '确定要删除' + that.data.data.houseName + '-' + that.data.data.roomcode + '吗？',
      success: function (res) {
        if (res.confirm) {
          common._get('/wx/room/delete', { roomId: that.data.id },
            //成功
            function (res) {
              console.log(res.data);
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: '删除成功',
                })
                wx.setStorageSync('modifyRoom', 'modifyRoom')
                wx.navigateBack({
                })

              }
            });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.room_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this;
    this.setData({
      value: [9999, this.data.month - 1, this.data.day - 1],
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var modify = wx.getStorageSync('modifyRoom');
    // wx.removeStorageSync('modifyRoom');
    // if (modify){
    this.initData();
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})